//
//  UIECGControlTableViewCell.m
//  vhECGTools
//
//  Created by jia yu on 2021/11/23.
//

#import "UIECGControlTableViewCell.h"
#import "UISettingsColorPickerViewController.h"
#import "vhECGTools/vhECGTools.h"
#import "UIECGSettingsView.h"


@interface UIECGControlTableViewCell()<UIColorPickerViewControllerDelegate,UITextFieldDelegate>{
    UIButton *colorButton;
    UITextField *widthTextField;
}
@end

@implementation UIECGControlTableViewCell
@synthesize tableView;
@synthesize cellKey;
@synthesize handler;

-(void)setCellKey:(NSString *)_cellKey{
    cellKey = _cellKey;
    colorButton.backgroundColor =  [tableView.settingsView colorWithKey:cellKey];
    float width = [tableView.settingsView widthWithKey:cellKey];
    widthTextField.text = [NSString stringWithFormat:@"%.1f", width];
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.layer.masksToBounds = YES;
        self.userInteractionEnabled = YES;
        self.textLabel.userInteractionEnabled = NO;
        self.detailTextLabel.userInteractionEnabled = NO;
        
        colorButton = [[UIButton alloc] initWithFrame:CGRectMake(140.f, 3.f, 40.f, self.frame.size.height-6.f)];
        colorButton.layer.masksToBounds = YES;
        colorButton.layer.cornerRadius = 3.f;
        colorButton.layer.borderColor = [UIColor grayColor].CGColor;
        colorButton.layer.borderWidth = 1.f;
        colorButton.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        colorButton.backgroundColor = [UIColor clearColor];
        [colorButton addTarget:self action:@selector(colorButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:colorButton];
        
        widthTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.f + CGRectGetMaxX(colorButton.frame), colorButton.frame.origin.y, 40.f, colorButton.frame.size.height)];
        widthTextField.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        widthTextField.layer.borderColor = [UIColor grayColor].CGColor;
        widthTextField.layer.borderWidth = 1.f;
        widthTextField.layer.cornerRadius = 3.f;
        widthTextField.layer.masksToBounds = YES;
        widthTextField.delegate = self;
        [self addSubview:widthTextField];
        
    }
    return self;
}

-(void)colorButtonAction{
    UIViewController *rootViewController = [vhECGTools currentRootVC];
    UISettingsColorPickerViewController *v = [[UISettingsColorPickerViewController alloc] init];
    v.fromView = rootViewController.view;
    v.fromViewRect = [colorButton.superview convertRect:colorButton.frame toView:rootViewController.view];
    v.delegate = self;
    [rootViewController presentViewController:v animated:YES completion:nil];
    return;
}

#pragma mark - UIColorPickerViewControllerDelegate
- (void)colorPickerViewController:(UIColorPickerViewController *)viewController
                   didSelectColor:(UIColor *)color
                     continuously:(BOOL)continuously API_AVAILABLE(ios(15.0)){
    colorButton.backgroundColor = color;
    if (handler){
        handler(cellKey, [widthTextField.text floatValue], colorButton.backgroundColor);
    }
    return;
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (handler){
        handler(cellKey, [widthTextField.text floatValue], colorButton.backgroundColor);
    }
    return;
}

@end
